package indi.wshape1.takeawaydeliveryinfo.listener;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-01-20
 */

@Component
public class MySessionListener implements HttpSessionListener {

    private int count = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        count++;
        se.getSession().getServletContext().setAttribute("onlineCount", count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

        HttpSession session = se.getSession();

        count--;
        session.getServletContext().setAttribute("onlineCount", count);

        if(session.getAttribute("phone") != null) {
            session.getServletContext().removeAttribute((String) session.getAttribute("phone"));
        }

    }
}
